package com.rayenterprise.kms.server;

import java.util.List;

import javax.servlet.ServletException;

import net.sf.gilead.core.PersistentBeanManager;
import net.sf.gilead.core.hibernate.HibernateUtil;
import net.sf.gilead.core.store.stateless.StatelessProxyStore;
import net.sf.gilead.gwt.PersistentRemoteService;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.rayenterprise.kms.client.remote.InfoRemote;
import com.rayenterprise.kms.domain.KInfo;


public class InfoRemoteService extends PersistentRemoteService implements
		InfoRemote {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		super.init();

		// Bean Manager initialization
		//
		HibernateUtil persistenceUtil = new HibernateUtil();
		persistenceUtil.setSessionFactory(HibernateContext.getSessionFactory());

		PersistentBeanManager beanManager = new PersistentBeanManager();
		beanManager.setPersistenceUtil(persistenceUtil);
		beanManager.setProxyStore(new StatelessProxyStore());
		setBeanManager(beanManager);

	}
	
	/**
	 * This is method is rarely used,put here just to make the class complete
	 * 
	 * @author h.r.inc
	 */
	@Override
	public List<KInfo> getAll() {
		// TODO Auto-generated method stub
		Session session = null;
		StringBuffer sb = new StringBuffer("");
		sb.append("from KInfo");
		try {
			session = HibernateContext.getSessionFactory().openSession();
			Query q = session.createQuery(sb.toString());
			return q.list();
		} catch (RuntimeException e) {

			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public KInfo getInfo(int infoId) {
		Session session = null;
		StringBuffer sb = new StringBuffer("");
		sb.append("from KInfo where infoId =");
		sb.append(infoId);
		try {
			session = HibernateContext.getSessionFactory().openSession();
			Query q = session.createQuery(sb.toString());
			return (KInfo) q.uniqueResult();
		} catch (RuntimeException e) {

			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public KInfo save(KInfo info) {
		Session session = null;
		Transaction transaction = null;
		
		try {
			session = HibernateContext.getSessionFactory().openSession();
			transaction = session.getTransaction();
			transaction.begin();
			session.saveOrUpdate(info);
			transaction.commit();
			return info;
		} catch (RuntimeException e) {
			transaction.rollback();
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public void deleteInfo(KInfo info) {
		Session session = null;
		Transaction transaction = null;
		
		try {
			session = HibernateContext.getSessionFactory().openSession();
			transaction = session.getTransaction();
			transaction.begin();
			session.delete(info);
			transaction.commit();			
		} catch (RuntimeException e) {
			transaction.rollback();
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

}
